<template>
  <div class="seckill-view">
    <div class="seckill-header">
      <h1 class="seckill-title">秒杀专区</h1>
      <div class="search-box">
        <!-- <el-input
          v-model="searchKeyword"
          placeholder="搜索秒杀商品"
          clearable
          @keyup.enter="handleSearch"
        >
          <template #suffix>
            <el-icon class="search-icon" @click="handleSearch"><Search /></el-icon>
          </template>
        </el-input> -->
      </div>
    </div>

    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else class="seckill-grid">
      <div v-for="product in seckillProducts" :key="product.id" class="seckill-product-card">
        <div class="product-image">
          <img :src="product.image" :alt="product.name">
        </div>
        <div class="product-info">
          <h3 class="product-name">{{ product.name }}</h3>
          <p class="product-description">{{ product.description }}</p>
          <div class="price-info">
            <span class="original-price">原价: ¥{{ product.originalPrice }}</span>
            <span class="seckill-price">秒杀价: ¥{{ product.price }}</span>
          </div>
          <div class="stock-info">
            <span class="stock">库存: {{ product.stock }}</span>
          </div>
          <button class="seckill-btn" @click="handleSeckill(product.id)">
            立即抢购
          </button>
        </div>
      </div>
    </div>
    <BackToTop />
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import BackToTop from '@/components/BackToTop.vue'
import { ElMessage } from 'element-plus'
import { getSeckillList, doSeckill, searchSeckillProducts } from '@/axios/seckill'

const router = useRouter()
const loading = ref(false)
const error = ref(null)
const searchKeyword = ref('')
const seckillProducts = ref([])

// 获取秒杀商品列表
const fetchSeckillProducts = async () => {
  loading.value = true
  try {
    const data = await getSeckillList()
    // 过滤掉状态为0的已下架商品
    seckillProducts.value = data.filter(product => product.status !== 0)
  } catch (err) {
    error.value = '获取秒杀商品失败'
    console.error('获取秒杀商品失败:', err)
  } finally {
    loading.value = false
  }
}

// 处理秒杀
const handleSeckill = async (productId) => {
  if (!productId) {
    ElMessage.error('商品ID不能为空')
    return
  }
  
  try {
    const res = await doSeckill(productId)
    if (res.code === 1) {
      ElMessage.success('抢购成功！')
      // 刷新商品列表
      fetchSeckillProducts()
    } else {
      ElMessage.error(res.msg || '抢购失败，请稍后重试')
    }
  } catch (err) {
    ElMessage.error(err.message || '抢购失败，请稍后重试')
  }
}

// 搜索功能
const handleSearch = async () => {
  if (!searchKeyword.value.trim()) {
    fetchSeckillProducts()
    return
  }
  
  loading.value = true
  try {
    const data = await searchSeckillProducts(searchKeyword.value)
    // 过滤掉状态为0的已下架商品
    seckillProducts.value = data.filter(product => product.status !== 0)
  } catch (err) {
    error.value = '搜索失败'
    console.error('搜索失败:', err)
  } finally {
    loading.value = false
  }
}

// 监听搜索关键词变化
watch(searchKeyword, (newVal) => {
  if (!newVal) {
    fetchSeckillProducts()
  }
})

onMounted(() => {
  fetchSeckillProducts()
})
</script>

<style scoped>
.seckill-view {
  width: 100%;
  padding: 20px;
}

.seckill-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.seckill-title {
  font-size: 28px;
  color: #2c3e50;
  margin: 0;
}

.search-box {
  width: 300px;
}

.search-icon {
  cursor: pointer;
  font-size: 20px;
  color: #409EFF;
}

.seckill-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  padding: 20px 0;
}

.seckill-product-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.seckill-product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.product-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 20px;
}

.product-name {
  font-size: 18px;
  color: #2c3e50;
  margin: 0 0 10px 0;
}

.product-description {
  font-size: 14px;
  color: #666;
  margin: 0 0 15px 0;
  display: -webkit-box;
  /* -webkit-line-clamp: 2; */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.price-info {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 15px;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.seckill-price {
  font-size: 20px;
  color: #e74c3c;
  font-weight: bold;
}

.stock-info {
  margin-bottom: 15px;
}

.stock {
  font-size: 14px;
  color: #666;
}

.seckill-btn {
  width: 100%;
  padding: 12px;
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.seckill-btn:hover {
  background: #c0392b;
  transform: translateY(-2px);
}

.loading, .error {
  text-align: center;
  padding: 40px;
  font-size: 18px;
  color: #666;
}

.error {
  color: #e74c3c;
}

@media (max-width: 768px) {
  .seckill-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 20px;
  }
  
  .search-box {
    width: 100%;
  }
}
</style> 